package adapter.group_management;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.ArrayList;

import controller.group_management.PhotoAndVDOTabInGroupFeedFragmentAsync;
import custom_view.CompanyFeedItemImageView;
import model.group_management.PhotoAndVDOIsCheckedInGroupFeedItem;
import model.group_management.PhotoAndVDOTabInGroupFeedItem;
import ui.activity.group_management.GroupFeed;
import ui.activity.group_management.PreviewPhotoAndVDOTabInGroupFeed;


/**
 * Created by trek2000 on 21/8/2014.
 */
public class PhotoAndVDOTabInGroupFeedFragmentAdapter extends BaseAdapter {

    /**
     * Data section
     */
    private ArrayList<PhotoAndVDOTabInGroupFeedItem> mAlPhotoAndVDOTabInGroupFeedItem = new ArrayList<>();

    /**
     * Interface section
     */

    /**
     * String section
     */
    private int textViewResourceId;
    /**
     * The other section
     */
    private Context mContext;
    private DisplayImageOptions mDio;
    private ViewHolder viewHolder;
    private GridViewPhotoAndVDOTabInGroupFeedFragmentAdapter gridViewPhotoAndVDOTabInGroupFeedFragmentAdapter = null;
    private ArrayList <PhotoAndVDOIsCheckedInGroupFeedItem> mAlPhotoAndVDOIsCheckedInGroupFeed =
            new ArrayList<>();


    private class ViewHolder {
        private GridView mGv;
        private TextView mTvElapsedTime;
        private TextView mTvFileNumber;
    }

    /**
     * @param context
     */
    public PhotoAndVDOTabInGroupFeedFragmentAdapter(Context context, int textViewResourceId, ArrayList<PhotoAndVDOTabInGroupFeedItem> mAlphotoAndVDOTabInGroupFeedItem) {
        this.mContext = context;
        this.textViewResourceId = textViewResourceId;
        this.mAlPhotoAndVDOTabInGroupFeedItem = mAlphotoAndVDOTabInGroupFeedItem;

        mDio = new DisplayImageOptions.Builder()
                .delayBeforeLoading(1000)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    /**
     * @return
     */
    public int getCount() {
        return mAlPhotoAndVDOTabInGroupFeedItem.size();
    }

    /**
     * @param position
     * @return
     */
    public PhotoAndVDOTabInGroupFeedItem getItem(int position) {
        return mAlPhotoAndVDOTabInGroupFeedItem.get(position);
    }

    /**
     * @param position
     * @return
     */
    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int pos, View convertView, ViewGroup parent) {

        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(textViewResourceId, null);
            viewHolder.mGv= (GridView) convertView.findViewById(R.id.gv_in_simple_list_photo_and_vdo_tab_in_group_feed_fragment);
            viewHolder.mTvElapsedTime = (TextView) convertView.findViewById(R.id.tv_elapsed_time_in_simple_list_photo_and_vdo_tab_in_group_feed_fragment);
            viewHolder.mTvFileNumber = (TextView) convertView.findViewById(R.id.tv_file_number_in_simple_list_photo_and_vdo_tab_in_group_feed_fragment);

            viewHolder.mGv.setTag(pos);
            viewHolder.mTvElapsedTime.setTag(pos);
            viewHolder.mTvFileNumber.setTag(pos);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();

            viewHolder.mGv.setTag(pos);
            viewHolder.mTvElapsedTime.setTag(pos);
            viewHolder.mTvFileNumber.setTag(pos);
        }
        //set data
        viewHolder.mTvElapsedTime.setText(
                mAlPhotoAndVDOTabInGroupFeedItem.get(pos).getAddedDate());
        viewHolder.mTvFileNumber.setText(
                String.valueOf(mAlPhotoAndVDOTabInGroupFeedItem.get(pos).getNumberOfPhotos()) + " " +
                        mContext.getString(R.string.photos_) + ", " +
                String.valueOf(mAlPhotoAndVDOTabInGroupFeedItem.get(pos).getNumberOfVideos()) + " " +
                        mContext.getString(R.string.videos_));
        gridViewPhotoAndVDOTabInGroupFeedFragmentAdapter = new GridViewPhotoAndVDOTabInGroupFeedFragmentAdapter(
                mContext, mAlPhotoAndVDOTabInGroupFeedItem.get(pos).getmAlShotOfPhotoAndVDOTabInGroupFeedItem());
        viewHolder.mGv.setAdapter(gridViewPhotoAndVDOTabInGroupFeedFragmentAdapter);
        //on click listener
        viewHolder.mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View v, int pos_of_gv, long l) {
                if (Enterprise.groupManagement.
                        getmAlPhotoAndVDOIsCheckedInGroupFeed() != null &&
                        Enterprise.groupManagement.
                        getmAlPhotoAndVDOIsCheckedInGroupFeed().size() > 0) {
                    setOnItemLongClickInPhotoAndVDOTabInGroupFeedFragment(
                            PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed,
                            mAlPhotoAndVDOIsCheckedInGroupFeed,
                            v, pos, pos_of_gv);
                } else {
                    Enterprise.groupManagement.setPositionOfPhotoAndVdoTabInGroupFeedItem(pos);
                    Enterprise.groupManagement.setPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem(pos_of_gv);
                    mContext.startActivity(new Intent(mContext, PreviewPhotoAndVDOTabInGroupFeed.class));
                }
            }
        });
        viewHolder.mGv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View v, int pos_of_gv, long l) {
                //clear data mAlPhotoAndVDOIsCheckedInGroupFeed after user click cancel and long item click again
                if (Enterprise.groupManagement.getmAlPhotoAndVDOIsCheckedInGroupFeed() == null  ||
                        (Enterprise.groupManagement.getmAlPhotoAndVDOIsCheckedInGroupFeed() != null
                                &&
                                Enterprise.groupManagement.getmAlPhotoAndVDOIsCheckedInGroupFeed().size() == 0)) {
                    mAlPhotoAndVDOIsCheckedInGroupFeed.clear();
                }

                setOnItemLongClickInPhotoAndVDOTabInGroupFeedFragment(
                        PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed,
                        mAlPhotoAndVDOIsCheckedInGroupFeed,
                        v, pos, pos_of_gv);
                //return true, prevent event on item click after on item long click
                return true;
            }
        });
        return convertView;
    }

    private void setOnItemLongClickInPhotoAndVDOTabInGroupFeedFragment (
            ArrayList<PhotoAndVDOTabInGroupFeedItem> mAlPhotoAndVDOTabInGroupFeed,
            ArrayList <PhotoAndVDOIsCheckedInGroupFeedItem> mAlPhotoAndVDOIsCheckedInGroupFeed,
            View v , int pos, int pos_of_gv) {

        if (!mAlPhotoAndVDOTabInGroupFeed.get(pos).
                getmAlShotOfPhotoAndVDOTabInGroupFeedItem().
                get(pos_of_gv).getIsChecked()) {
            //update UI
            ((CompanyFeedItemImageView)v).setChecked(true);
            // update mAlPhotoAndVDOIsCheckedInGroupFeed
            mAlPhotoAndVDOIsCheckedInGroupFeed.add(
                    new PhotoAndVDOIsCheckedInGroupFeedItem(
                            pos, pos_of_gv));
            //update mAlPhotoAndVDOTabInGroupFeed
            mAlPhotoAndVDOTabInGroupFeed.get(pos).
                    getmAlShotOfPhotoAndVDOTabInGroupFeedItem().
                    get(pos_of_gv).setIsChecked(true);
        } else {
            //update UI
            ((CompanyFeedItemImageView)v).setChecked(false);
            // update mAlPhotoAndVDOIsCheckedInGroupFeed
            for (int i = 0 ; i < mAlPhotoAndVDOIsCheckedInGroupFeed.size(); i++) {
                if (mAlPhotoAndVDOIsCheckedInGroupFeed.get(i).
                        getPositionOfPhotoAndVdoIsCheckedInGroupFeedItem() == pos &&
                        mAlPhotoAndVDOIsCheckedInGroupFeed.get(i).
                                getPositionOfGridViewOfPhotoAndVdoIsCheckedInGroupFeedItem() == pos_of_gv) {
                    mAlPhotoAndVDOIsCheckedInGroupFeed.remove(i);
                }
            }
            //update mAlPhotoAndVDOTabInGroupFeed
            mAlPhotoAndVDOTabInGroupFeed.get(pos).
                    getmAlShotOfPhotoAndVDOTabInGroupFeedItem().
                    get(pos_of_gv).setIsChecked(false);
        }
        //save mAlPhotoAndVDOIsCheckedInGroupFeed to single ton
        Enterprise.groupManagement.setmAlPhotoAndVDOIsCheckedInGroupFeed(mAlPhotoAndVDOIsCheckedInGroupFeed);
        //update UI ll download
        if (mAlPhotoAndVDOIsCheckedInGroupFeed.size() > 0) {
            //hide ll upload and show ll download
            GroupFeed.mLLDownloadInGroupFeed.setVisibility(View.VISIBLE);
            GroupFeed.mLLUploadInGroupFeed.setVisibility(View.GONE);
            //update text view download
            GroupFeed.mTvDownloadInGroupFeed.setText(
                    mContext.getString(R.string.download) + "(" +
                            mAlPhotoAndVDOIsCheckedInGroupFeed.size() + ")"
            );
        } else {
            //hide ll download and show ll upload
            GroupFeed.mLLDownloadInGroupFeed.setVisibility(View.GONE);
            GroupFeed.mLLUploadInGroupFeed.setVisibility(View.VISIBLE);
        }
    }
}